<?php 
/**
* 
*/
class AttendenceController{
	
	function log(){
		//根据接收的班级名称，并根据session查得教师的课程，显示该班级的考核表格
        $class = isset($_GET['class'])?$_GET['class']:"";
        $attend = new AttendModel;
        $row1=$attend->getFields($class);
        $fields = array();
        foreach ($row1 as $v) {
            $fields[] = $v['column_name'] ;
        }
        $roleuser = new RoleUserModel;
        $role_id=$roleuser->getRoleId();
        $tc = new TeacherCourseModel;
        $course = $tc->getCourse($role_id,$class);
        $c = array();
        foreach ($course as $key=>$v) {
            $c[] = $v['cno'];
        }
        //var_dump($c);
        $len = count($c);
        //echo $len;

        $c1 = implode(",", $c);
        //if(!empty($class)){
            $row = $attend->studentlist($class,$c1);
        //}
        
        include(ROOT.'view/admin/tpl/student_list.html');
        
	}
	function logAction(){
        //var_dump($_GET);
        $course = isset($_GET['course'])?$_GET['course']:"";
        $id = isset($_GET['id'])?$_GET['id']:"";
        $key = isset($_GET['key'])?$_GET['key']:"";
        $bj = isset($_GET['bj'])?$_GET['bj']:"";
        $attend = new AttendModel;
        
        if($attend->attend($key,$course,$id,$bj)){
            echo 1;
        }
		//处理考勤数据的登入
	}

    function logAction2(){
       
        $c1 = isset($_GET['c1'])?$_GET['c1']:"";
        $id = isset($_GET['id'])?$_GET['id']:"";
        $c2 = isset($_GET['c2'])?$_GET['c2']:"";
        $bj = isset($_GET['bj'])?$_GET['bj']:"";
        $key1 = isset($_GET['key1'])?$_GET['key1']:"";
        $key2 = isset($_GET['key2'])?$_GET['key2']:"";
        $attend = new AttendModel;
        
        if($attend->attend2($c1,$c2,$id,$bj,$key1,$key2)){
            echo 1;
        }
        //处理考勤数据的登入
    }

    function Image(){
        $img = new ImageTool;
        $a = $img->captcha();
    }

	function tableCreate(){
		
		include(ROOT.'view/admin/tpl/table_create.html');

	}

	function tableImport(){
		spl_autoload_unregister('__autoloadmy');
		require_once ROOT.'plugins/PHPExcel.php';
	    require_once ROOT.'plugins/PHPExcel/IOFactory.php';
	    require_once ROOT.'plugins/PHPExcel/Reader/Excel5.php';

	    $con = mysql_connect("localhost","root","");
		mysql_select_db('kq');
		mysql_query("set names utf8");
	    $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format 
        
        $objPHPExcel = PHPExcel_IOFactory::load($_FILES["inputExcel"]["tmp_name"]);
        $sheet = $objPHPExcel->getSheet(0); 
        $highestRow = $sheet->getHighestRow();           //取得总行数 
        $highestColumn = $sheet->getHighestColumn(); 
	    $objWorksheet = $objPHPExcel->getActiveSheet();
        $highestRow = $objWorksheet->getHighestRow(); 
        $highestColumn = $objWorksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
        $headtitle=array(); 
        $strs1=array();
        $strs2=array();
        $sql=array();
        $table = substr($_FILES["inputExcel"]["name"],0,strrpos($_FILES["inputExcel"]["name"], "."));

        for ($col1 = 2;$col1 < $highestColumnIndex;$col1++)
        {
        	if($objWorksheet->getCellByColumnAndRow($col1, 1)->getValue()!=''){
            	$strs1[$col1] ="`".$objWorksheet->getCellByColumnAndRow($col1, 1)->getValue()."` VARCHAR(11) DEFAULT '0' ";
        		$strs2[$col1] =$objWorksheet->getCellByColumnAndRow($col1, 1)->getValue();
        	}
       
        } 
        $s = implode(",",$strs1);
        $s2 = implode(",",$strs2);
        $sql[] ="DROP TABLE IF EXISTS `".$table."`;";
        $sql[] = "
				CREATE TABLE `".$table."` (
				  `id` INT(11) NOT NULL AUTO_INCREMENT ,
				  `NO` VARCHAR(11) NOT NULL ,
				  `name` VARCHAR(64) NOT NULL,
				  $s,
				  PRIMARY KEY (`id`)
				) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;";
        foreach ($sql as $v) {
        	mysql_query($v);
        }
       
        for ($row = 2;$row <= $highestRow;$row++) 
        {
            $strs=array();
           
            //注意highestColumnIndex的列数索引从0开始
            for ($col = 0;$col < $highestColumnIndex;$col++)
            {
            	if($objWorksheet->getCellByColumnAndRow($col, $row)->getValue()!=''|| $objWorksheet->getCellByColumnAndRow($col, $row)->getValue()=='0'){
					$strs[$col] ="'".$objWorksheet->getCellByColumnAndRow($col, $row)->getValue()."'";	  
			    }
            } 
            $s3 = implode(",",$strs);
            if(!empty($s3)){
                $sql1 = "INSERT INTO ".$table."(NO,name,$s2) VALUES ($s3)";
                if(!mysql_query($sql1))
                { 
                    echo '<script>';
                    echo  "alert('导入失败，请重新导入');";
                    echo "window.location.href='./index.php?c=init&a=tableCreate';";
                    echo '</script>';
                    exit;
                }
            }
            
        }
        echo '<script>';
        echo  "alert('导入成功');";
        echo "window.location.href='./index.php/init/tableCreate';";
        echo '</script>';
	    spl_autoload_register('__autoloadmy'); 
	}
}

 ?>